﻿using System.Xml.Serialization;

namespace z.m{
	[XmlRoot(ElementName="Z")]
	public class StatusT:IXmlSerializable{
		private c.ZSession Ses;
		private int R;
		private int status;
		public StatusT() {
		}
		public StatusT(c.ZSession Ses=null,int R=-2) {
			this.Ses = Ses;
			status = 0;
			if (Ses != null) {
				if (Ses.User != null) status++;
				if (Ses.GameType != null) status++;
				if (Ses.Room != null) status++;
			}
			this.R = R==-2?status:R;
		}
		public System.Xml.Schema.XmlSchema GetSchema() {
			return null;
		}

		public void ReadXml(System.Xml.XmlReader reader) {
			
		}

		public void WriteXml(System.Xml.XmlWriter writer) {
			if (R == -1 || R == 1) {
				writer.WriteStartElement("S");
				writer.WriteAttributeString("c", status.ToString());
				writer.WriteAttributeString("r", R.ToString());
				if (status > 0) {
					writer.WriteStartElement("U");
					writer.WriteAttributeString("i", Ses.User.ID.ToString());
					writer.WriteAttributeString("n", Ses.User.Acc);
					writer.WriteEndElement();
				}
				if (status > 1) {
					writer.WriteStartElement("G");
					//writer.WriteAttributeString("i", Ses.GameType.ID.ToString());
					writer.WriteAttributeString("n", Ses.GameType.Name);
					writer.WriteEndElement();
				}
				if (status > 2) {
					writer.WriteStartElement("R");
					writer.WriteAttributeString("i", Ses.Room.ID.ToString());
					writer.WriteEndElement();
				}
				writer.WriteEndElement();
			}
			if (R == -1 || R == 2) {
				writer.WriteStartElement("Gs");
				foreach (var x in c.App.Games) {
					writer.WriteStartElement("G");
					writer.WriteAttributeString("i", x.ID.ToString());
					writer.WriteAttributeString("n", x.Name);
					writer.WriteEndElement();
				}
				writer.WriteEndElement();
			}
			if (R == -1 || R == 3) {
				writer.WriteStartElement("Rs");
				foreach (var x in c.App.Rooms) {
					writer.WriteStartElement("R");
					writer.WriteAttributeString("i", x.ID.ToString());
					writer.WriteEndElement();
				}
				writer.WriteEndElement();
			}
		}
	}
}